package org.lql.chaos.rocketmq.service.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: liangqinglong
 * @date: 2025-08-16 20:19
 * @description: 定时/延时消息生产者
 **/
@Slf4j
@Service
public class ScheduleProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送延时消息
     *
     * @param topic       主题
     * @param tag         标签
     * @param msg         消息内容
     * @param delayLevel  延时级别 (1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h)
     */
    public void sendDelayMessage(String topic, String tag, String msg, int delayLevel) {
        String destination = topic + ":" + tag;
        log.info("发送延时消息：{}", msg);
        
        // 使用RocketMQ原生Message对象并设置延时级别
        Message message = new Message(topic, tag, msg.getBytes());
        message.setDelayTimeLevel(delayLevel);
        
        // 使用convertAndSend发送消息
        rocketMQTemplate.convertAndSend(destination, message);
        log.info("延时消息发送完成");
    }
}